package 热题100;

import java.util.HashMap;

public class 热题1两数之和 {

    /**
     * 在数组找到两数之和为target的两个值，返回其下标
     * @param nums 数组
     * @param target 目标值
     * @return 返回下标数组
     */
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> map = new HashMap<>();
        int[] arr = new int[2];
        int index = 0;
        map.put(nums[0],0);//将值作为key，下标作为value
        for (int i = 1; i < nums.length; i++) {
            if (map.containsKey(target-nums[i])){
                arr[index] = map.get(target-nums[i]);
                arr[++index] = i;
                return arr;
            }else {
                map.put(nums[i],map.getOrDefault(nums[i],0)+1);
            }
        }
        return arr;
    }
}
